if not pattern then pattern = {} end

if not pattern.freeze then

pattern.freeze = {last_time = {}}

if not ScriptsRootPath then
	ScriptsRootPath = getScriptPath() .. "\\..\\"
end

pattern.freeze.patterns_init = lib.path.init .. "patterns.ini"

dofile (ScriptsRootPath .. "lib\\lib.lua")

function pattern.freeze.Init()
	pattern.freeze.params = lib:LoadIni(pattern.freeze.patterns_init, "freeze", {range = 0.5, period = 60})
	if processor and processor.log then
		processor:log("pattern.freeze.params", pattern.freeze.params)
	end
end

function pattern.freeze.OnCandle(source, index, data)
	if not source then return end
	if not index then index = 1 end
	if not pattern.freeze.params then pattern.freeze.Init() end
	--[ [
	if pattern.freeze.trade_date ~= lib:FormatDate() then
		pattern.freeze.notify_time = lib:getTradeDate()
		pattern.freeze.trade_date = lib:FormatDate()
		pattern.freeze.notify_time.hour = 10
		pattern.freeze.notify_time.min = 0
		pattern.freeze.notify_time.sec = 0
		pattern.freeze.notify_time.ms = 0
		pattern.freeze.notify_time.mcs = 0
		
		pattern.freeze.notify_time = lib:tickseconds(pattern.freeze.notify_time) + pattern.freeze.params.period * 60 * 7
	end
	--]]
	local candle0 = lib:GetLastCandle(source, index)
	local candle1 = lib:GetLastCandle(source, index + 1)
	if candle0 and candle1
		and lib:isFile(lib.path.log .. "MOVE." .. candle0.instrument.code .. "." .. lib:FormatDate() .. ".log")
	then
		local datetime = lib:tickseconds(candle0.datetime)
		local datetime1 = lib:tickseconds(candle1.datetime)
		local sec = datetime - datetime1
		local period = math.ceil(pattern.freeze.params.period * 60 / sec)
		if period <= 0 then period = 1 end
		
		local pmin, pmax, is_long = lib:GetCandleRange(source, index, period, true)
		if pmin then
			local percent = round((pmax - pmin) * 100 / pmin, 2)
			
			if percent <= pattern.freeze.params.range then
				if not pattern.freeze.last_time[candle0.instrument.code] then
					pattern.freeze.last_time[candle0.instrument.code] = {}
				end
				
				pattern.freeze.last_time[candle0.instrument.code][datetime] = is_long
				
				if not pattern.freeze.last_time[candle0.instrument.code][datetime1]
				then
					local price
				
					if is_long then
						price = pmax
					else
						price = pmin
						percent = -percent
					end
										
					datetime = candle0.datetime
					datetime.is_long__ = is_long
					
					datetime.hint__ = string.format("FREEZE %s %s%%",
						lib:FormatPrice(price, candle0.instrument),
						--lib:bool2side(is_long),
						tostring(percent)
					)
					if (not pattern.freeze.notify_time
						or pattern.freeze.notify_time >= lib:tickseconds(datetime))
						--and lib:isFile(lib.path.log .. "MOVE." .. candle0.instrument.code .. "." .. lib:FormatDate() .. ".log")
						and not lib:isFile(lib.path.log .. "FREEZE." .. candle0.instrument.code .. "." .. lib:FormatDate() .. ".log")
					then
						datetime.notify__ = candle0.instrument.code .. " " .. datetime.hint__
						
						--return price, datetime
					end
					lib:AddLine(self.path.log .. "FREEZE." .. candle0.instrument.code .. "." .. lib:FormatDate() .. ".log", datetime.hint__)

					return price, datetime
				end
			end
		end
	end
end

end -- of file
